package com.iteima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iteima.reggie.common.CustomException;
import com.iteima.reggie.common.R;
import com.iteima.reggie.dto.DishDto;
import com.iteima.reggie.entity.Dish;
import com.iteima.reggie.entity.DishFlavor;
import com.iteima.reggie.mapper.DishMapper;
import com.iteima.reggie.service.DishFlavorService;
import com.iteima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;
    /**
     * 新增菜品,同时保存对应的口味数据
     * @param dishDto
     */
//    @Transactional 在Service层使用了
    public void saveWithFlavor(DishDto dishDto) {
        //保持菜品的基本信息到菜品表dish
        this.save(dishDto);
        Long disId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
//        flavors.forEach(f ->f.setDishId(disId));
//        flavors.stream().distinct().forEach(s->s.setDishId(disId));
        //把上面的菜品id插进口味表中,因为此时的口味表中的菜品id为空
        flavors.stream().map(s ->{
            s.setDishId(disId);
            return s;
        }).collect(Collectors.toList());
        log.info(flavors.toString());

        //保存菜品口味数据到口味表dish_flavor
        dishFlavorService.saveBatch(flavors);

    }

    /**
     *   根据菜品id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品信息,从dish上查询
        Dish dish = getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询菜品对应的口味信息,从dish_flavor表上查询
        LambdaQueryWrapper<DishFlavor>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(dishFlavorList);


        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //修改菜品口味表
        //先删除原本的菜口味表,
        LambdaQueryWrapper<DishFlavor>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //添加当前提交过来的口味数据--dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存新的口味数据
        dishFlavorService.saveBatch(flavors);
    }

    /**
     *     //通过ids修改status=id
     * @param ids
     * @param id
     */
    @Override
    public void upStatusWithIds(List<Long> ids, Integer status) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids != null && status != null,Dish::getId,ids);

        List<Dish> dishList = dishService.list(queryWrapper);


        if (dishList !=null && !dishList.isEmpty() ){
            for (Dish dish : dishList) {
                dish.setStatus(status);
            }
           dishService.updateBatchById(dishList);
        }else {
           throw new CustomException("没有找到该菜品");
        }

    }
}
